var recordsCount = 0, logRecordsCount = 0;
function showMessage(sendMessages, clickEvent) {
	
	var obj = jQuery.parseJSON(sendMessages);
	if(obj.subMessageType=='service'){
		var startServiceDebugger = $("#startServiceDebugger")[0].checked;
		if(startServiceDebugger){
			
			var filter_ipAdress = $("#filter_ipAddress").val();
			var filter_serviceId = $("#filter_serviceId").val();
			var filterPass = true;
			if(filter_ipAdress!='' && obj.ipAddress.indexOf(filter_ipAdress)<0){
				filterPass = false;
			}
			if(filter_serviceId!='' && obj.serviceId.indexOf(filter_serviceId)<0){
				filterPass = false;
			}
			
			if(filterPass){
				var content = '';
				content += '<tr name="recordsTr" ondblclick="detailView(this)">';
				content += '<td align="center" name="nodeId">'+obj.nodeId+'</td>';
				content += '<td align="center" name="ipAddress">'+obj.ipAddress+'</td>';
				content += '<td align="center" name="serviceId">'+obj.serviceId+'</td>';
				content += '<td align="center" name="invorkTime">'+obj.invorkTime+'</td>';
				content += '<td align="center" name="responseTime">'+obj.responseTime+'</td>';
				content += '<td align="center" name="esbLoginId">'+obj.esbLoginId+'</td>';
				content += '<td name="inParam" style="width:200px;white-space:pre;text-overflow:ellipsis;overflow:hidden;">'+obj.inParam+'</td>';
				var outParamObj = jQuery.parseJSON(obj.outParam);
				if(outParamObj.code<1){
					content += '<td name="outParam" class="red warn_bg" style="width:200px;white-space:pre;text-overflow:ellipsis;overflow:hidden;">'+obj.outParam+'</td>';
				}else{
					content += '<td name="outParam" style="width:200px;white-space:pre;text-overflow:ellipsis;overflow:hidden;">'+obj.outParam+'</td>';
				}
				content += '</tr>';
				$("#serviceLogTable tr").eq(0).after(content);
				recordsCount ++;
				$("#recordsCount").text(recordsCount);
			}
		}
	}else if(obj.subMessageType=='log'){
		var startLogDebugger = $("#startLogDebugger")[0].checked;
		var filter_level = $("input[name='filter_level']:checked").val();
		if(startLogDebugger){
			var filterPass = true;
			if(filter_level=='error' && obj.level!='ERROR'){
				filterPass = false;
			}else if(filter_level == 'warn'){
				if(obj.level != 'ERROR' && obj.level != 'WARN'){
					filterPass = false;
				}
			}else if(filter_level == 'info'){
				if(obj.level != 'ERROR' && obj.level != 'WARN' && obj.level != 'INFO'){
					filterPass = false;
				}
			}
			if(filterPass){
				var content = '';
				content += '<tr name="recordsTr" ondblclick="detailLogView(this)">';
				content += '<td align="center" name="nodeId">'+obj.nodeId+'</td>';
				if(obj.level=='ERROR'){
					content += '<td align="center" name="level" class="red warn_bg">'+obj.level+'</td>';
				}else{
					content += '<td align="center" name="level">'+obj.level+'</td>';
				}
				content += '<td align="center" name="date">'+obj.date+'</td>';
				content += '<td align="center" name="time">'+obj.time+'</td>';
				content += '<td name="message" style="width:400px;white-space:pre;text-overflow:ellipsis;overflow:hidden;">'+obj.message+'</td>';
				content += '<td name="className" style="width:350px;white-space:pre;text-overflow:ellipsis;overflow:hidden;">'+obj.className+'</td>';
				content += '</tr>';
				$("#logTable tr").eq(0).after(content);
				logRecordsCount ++;
				$("#recordsCount_log").text(logRecordsCount);
			}
		}
	}
	
}

function setFilter(){
	
	var filter_ipAdress = $("#filter_ipAddress").val();
	var filter_serviceId = $("#filter_serviceId").val();
	var content = '';
	content += '<div class="right-content">';
	content += '<div class="operate-nav">';
	content += '<div class="operate-contant">';
	content += '<table width="100%" border="0" cellspacing="0" cellpadding="0" >';
		
	content +='<tr>';
	content +='<td width="25%">发起服务器:</td>';
    content +='<td width="25%"><input type="text" id="ipAddress" name="ipAddress" class="width-180" value="'+filter_ipAdress+'"/></td>';
    content +='</tr>';
    
    content +='<tr>';
    content +='<td width="25%">服务名:</td>';
    content +='<td width="25%"><input type="text" id="serviceId" name="serviceId" class="width-180"  value="'+filter_serviceId+'"/></td>';
    content +='</tr>';
    
    content +='</table>';
    content +='</div>';
    content +='<div class="mt10 mb10 tr"><button class="btn-32" id="add_btn">确定</button></div>';
    content +='</div>';
    content +='</div>';
    apexLayer.OpenFrame({
		title:'过滤条件[支持模糊]',
		width:400,
		height:170,
		maxmin:false,
		closeBtn:1,
		scrollbar:false,
		content:content
		
	});          
	//自设关闭
	$('#add_btn').on('click', function(){
		
		var ipAddress = $("#ipAddress").val();
		var serviceId = $("#serviceId").val();
		$("#filter_ipAddress").val(ipAddress);
		$("#filter_serviceId").val(serviceId);
		var filterContent = '';
		if(ipAddress!=''){
			filterContent += '【发起服务器：'+ipAddress+'】'
		}
		if(serviceId!=''){
			filterContent += '【服务名：'+serviceId+'】'
		}
		if(filterContent == ''){
			filterContent += '无'
		}
		$("#filterText").text(filterContent);
		apexLayer.CloseAll();
	});
	
}

function cleanServiceLogFilter(){
	$("#filter_ipAddress").val('');
	$("#filter_serviceId").val('');
	$("#filterText").text('无');
}

function cleanServiceLogRecords(){
	recordsCount = 0;
	$("#recordsCount").text(recordsCount);
	$("#serviceLogTable").find("tr[name='recordsTr']").remove();
}


function cleanLogRecords(){
	logRecordsCount = 0;
	$("#recordsCount_log").text(logRecordsCount);
	$("#logTable").find("tr[name='recordsTr']").remove();
}


function detailView(obj){
	
	var nodeId =$(obj).find('td[name="nodeId"]').text();
	var ipAddress =$(obj).find('td[name="ipAddress"]').text();
	var serviceId =$(obj).find('td[name="serviceId"]').text();
	var invorkTime =$(obj).find('td[name="invorkTime"]').text();
	var responseTime =$(obj).find('td[name="responseTime"]').text();
	var esbLoginId =$(obj).find('td[name="esbLoginId"]').text();
	var inParam =$(obj).find('td[name="inParam"]').text();
	var outParam =$(obj).find('td[name="outParam"]').text();
	var content = '';
	content += '<div class="right-content">';
	content += '<div class="operate-nav">';
	content += '<div class="operate-contant">';
	content += '<table width="100%" border="0" cellspacing="0" cellpadding="0" >';
	content += '<tr>';
	content +='<td width="15%">节点:</td>';
    content +='<td width="35%">'+nodeId+'</td>';
    content +='<td width="15%">发起服务器:</td>';
    content +='<td width="35%">'+ipAddress+'</td>';
    content +='</tr>';
    
    content +='<tr>';
    content +='<td>服务名:</td>';
    content +='<td>'+serviceId+'</td>';
    content +='<td>用户:</td>';
    content +='<td>'+esbLoginId+'</td>';
    content +='</tr>';
    content +='<tr>';
    content +='<td>时间:</td>';
    content +='<td>'+invorkTime+'</td>';
    content +='<td>响应时间:</td>';
    content +='<td>'+responseTime+'</td>';
    content +='</tr>';
    content +='</table>';
    content +='<table width="100%">';
   	content +='<tr>';
	content +='<td width="49.5%" valign="top">';
	content +='<div class="operate-title"><span>输入包内容</span></div>';
    content +='<div class="test-result">';
   	content +='<textarea id="inPack_area" name="" cols="" rows="">'+JsonUti.convertToString(jQuery.parseJSON(inParam))+'</textarea>';
   	content +='</div>';
    content +='</td>';
	content +='<td width="1%">&nbsp;</td>';
	content +='<td width="49.5%" valign="top">';
	content +='<div class="operate-title"><span>输出包内容</span></div>';
    content +='<div class="test-result">';
   	content +='<textarea id="result_area" name="" cols="" rows="">'+JsonUti.convertToString(jQuery.parseJSON(outParam))+'</textarea>';
   	content +='</div>';
    content +='</td>';
	content +='</tr>';
	content +='</table>';
    content +='</div>';
    content +='</div>';
    content +='</div>';
        
    var winWidth = $(window).width();
	var winHeight = $(window).height();
    apexLayer.OpenFrame({
		title:'日志详情',
		width:winWidth*0.8,
		height:winHeight*0.75,
		maxmin:false,
		closeBtn:1,
		scrollbar:false,
		content:content
		
	});
}

function detailLogView(obj){
	
	var nodeId =$(obj).find('td[name="nodeId"]').text();
	var level =$(obj).find('td[name="level"]').text();
	var date =$(obj).find('td[name="date"]').text();
	var time =$(obj).find('td[name="time"]').text();
	var className =$(obj).find('td[name="className"]').text();
	var message =$(obj).find('td[name="message"]').text();
	var content = '';
	content += '<div class="right-content">';
	content += '<div class="operate-nav">';
	content += '<div class="operate-contant">';
	content += '<table width="100%" border="0" cellspacing="0" cellpadding="0" >';
	content += '<tr>';
	content +='<td width="25%">节点:</td>';
    content +='<td width="75%">'+nodeId+'</td>';
    content +='</tr>';
    
    content +='<tr>';
    content +='<td width="25%">日志等级:</td>';
    content +='<td width="75%">'+level+'</td>';
    content +='</tr>';
    content +='<tr>';
    content +='<td>日期:</td>';
    content +='<td>'+date+'</td>';
    content +='</tr>';
    content +='<tr>';
    content +='<td>时间:</td>';
    content +='<td>'+time+'</td>';
    content +='</tr>';
    content +='<tr>';
    content +='<td>异常类:</td>';
    content +='<td>'+className+'</td>';
    content +='</tr>';
    content +='<tr>';
    content +='<td>日志内容:</td>';
    content +='<td><textarea rows="15">'+message+'</textarea></td>';
    content +='</tr>';
    content +='</table>';
    content +='</div>';
    content +='</div>';
    content +='</div>';
        
    var winWidth = $(window).width();
	var winHeight = $(window).height();
    apexLayer.OpenFrame({
		title:'日志详情',
		width:winWidth*0.8,
		height:winHeight*0.75,
		maxmin:false,
		closeBtn:1,
		scrollbar:false,
		content:content
		
	});
}

function showServiceLog(){
	changeTab("serviceTab");
}
function showLog(){
	changeTab("logTab");
}

function changeTab(tabName){
	var tabId = "#"+tabName;
	var containerId = "#"+tabName+"Container";
	$("#authTab").find("a").removeClass("cur");
	$(tabId).addClass("cur");
	$("#tabContainer").find("div[name='treeDiv']").addClass("hide");
	$(containerId).removeClass("hide");
	
}